package com.weiyin.examination.adapter;

import android.app.Activity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.module.SelectionTypeModule;
import com.weiyin.examination.util.LayoutHelper;

import java.util.List;

/**
 * @author njf
 * @date 2017/9/26
 */

public class CourseA1DialogAdapter extends BaseQuickAdapter<SelectionTypeModule, BaseViewHolder> {
    private Activity activity;
    private View oldView;
    private String type;

    public CourseA1DialogAdapter(Activity activity, @LayoutRes int layoutResId, @Nullable List<SelectionTypeModule> data, String type) {
        super(layoutResId, data);
        this.activity = activity;
        this.type = type;
    }

    @Override
    protected BaseViewHolder createBaseViewHolder(View view) {
        LayoutHelper layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(view);
        return super.createBaseViewHolder(view);
    }

    private OnItemClickInterface onItemClickInterface;

    public void setOnItemClickInterface(OnItemClickInterface onItemClickInterface) {
        this.onItemClickInterface = onItemClickInterface;
    }

    public interface OnItemClickInterface {
        void onItemClick(BaseViewHolder holder, int position, String result, String type);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final SelectionTypeModule item) {
        if (item != null) {
            if (item.getAnswer() != null) {
                if (item.getAnswer().contains("/")) {
                    helper.setVisible(R.id.courseA1DialogItemTextView1, false);
                    helper.setVisible(R.id.courseA1DialogItemRl, true);

                    String result = item.getAnswer().substring(0, item.getAnswer().indexOf("/"));
                    if (item.getAnswer().substring(0, item.getAnswer().indexOf("/")).contains("-")) {
                        helper.setText(R.id.courseA1DialogItemTextView2, result.substring(0, result.indexOf("-")) + "\n" + result.substring(result.indexOf("-") + 1));
                    } else {
                        helper.setText(R.id.courseA1DialogItemTextView2, item.getAnswer().substring(0, item.getAnswer().indexOf("/")));
                    }

                    String result1 = item.getAnswer().substring(item.getAnswer().indexOf("/") + 1);
                    if (result1.contains("-")) {
                        helper.setText(R.id.courseA1DialogItemTextView3, result1.substring(0, result1.indexOf("-")) + "\n" + result1.substring(result1.indexOf("-") + 1));
                    } else {
                        helper.setText(R.id.courseA1DialogItemTextView3, result1);
                    }
                } else {
                    helper.setVisible(R.id.courseA1DialogItemTextView1, true);
                    helper.setVisible(R.id.courseA1DialogItemRl, false);
                    if (item.getAnswer().contains("-")) {
                        helper.setText(R.id.courseA1DialogItemTextView1, item.getAnswer().substring(0, item.getAnswer().indexOf("-")) + "\n" + item.getAnswer().substring(item.getAnswer().indexOf("-") + 1));
                    } else {
                        helper.setText(R.id.courseA1DialogItemTextView1, item.getAnswer());
                    }
                }
            }
        }

        helper.getView(R.id.courseA1DialogItemLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oldView != null) {
                    oldView.setSelected(false);
                }
                v.setSelected(true);
                oldView = v;
                if (onItemClickInterface != null) {
                    onItemClickInterface.onItemClick(helper, helper.getAdapterPosition(), item.getAnswer(), type);
                }
            }
        });
    }
}
